//
//  NearHeadView.swift
//  YueChat
//
//  Created by 1 on 2024/10/28.
//

import UIKit

class NearHeadView: UITableViewHeaderFooterView,UICollectionViewDelegate, UICollectionViewDataSource {
    
    var data = [DMCCUser]()

    weak var delegate:GroupDetailHeadDelegate?
    
    lazy var layout: UICollectionViewFlowLayout = {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kScreenWidth/6, height: 80)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 5, left: 10, bottom: 10, right: 10)
        return layout
    }()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        return collectionView
    }()
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = UIColor.white
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self).offset(-20)
        }
        
        collectionView.register(UINib(nibName: "GroupDetailHeadImgCell", bundle: nil), forCellWithReuseIdentifier: "GroupDetailHeadImgCell")
    }
    
    func update () {
        self.collectionView.reloadData()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.backgroundColor = kGrayBG
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    @objc func moreClick () {
        self.delegate?.FriendSeeMoreBlock()
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data.count;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GroupDetailHeadImgCell", for: indexPath) as! GroupDetailHeadImgCell
        cell.nameLab.isHidden = false
        
        let user = data[indexPath.row]
//        let groupId = member.groupId
//        let memberId = member.memberId
        
//        let user = gosnUser.getUserInfoDefault(userInfo.userId)

        cell.nameLab.text = user.getAlias()
        cell.img.sd_setImage(with: URL(string: user.portrait), placeholderImage: SDImageDefault)

        let nftStr = user.getNft()
        cell.nftImg.isHidden = nftStr!.isEmpty

        return cell
    }
    
//    func isManager () -> Bool {
//        let str = self.info.getGroupJoinTypeWithGroupId()
//        return str == "admin"
//    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        self.delegate?.FriendSeeBlock(index: indexPath.row)
    }

}
